package com.ruoyi.system.controller.cust;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.MaterialExp;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.cust.BasCust;
import com.ruoyi.system.service.cust.IBasCustService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/system/cust")
public class BasCustCtr extends BaseController {
    @Autowired
    IBasCustService basCustService;

    @GetMapping("/list")
    public TableDataInfo selectCustList(BasCust basCust) {
        startPage();
        return getDataTable(basCustService.list(new QueryWrapper<>(basCust)));
    }


    @PostMapping
    @Log(title = "供应商信息", businessType = BusinessType.INSERT)
    public AjaxResult insertCust(@RequestBody BasCust basCust) {
        basCust.setCreateStaffId(SecurityUtils.getUserId());//获取创建人日期
        basCust.setCreateStaffName(SecurityUtils.getUsername());
        basCust.setCreateDate(LocalDateTime.now());
        return toAjax(basCustService.save(basCust) ? 1 : 0);
    }

    @GetMapping(value = "/{custId}")
    public AjaxResult getInfo(@PathVariable("custId") Long custId){
        return success(basCustService.getById(custId));
    }

    @PutMapping
    @Log(title = "供应商信息", businessType = BusinessType.UPDATE)
    public AjaxResult updateCust(@RequestBody BasCust basCust) {
        UpdateWrapper<BasCust> queryWrapper = new UpdateWrapper<BasCust>();
        queryWrapper.lambda().set(BasCust::getCustName, basCust.getCustName())
                .set(BasCust::getInvoice, basCust.getInvoice())
                .set(BasCust::getCustNo, basCust.getCustNo())
                .set(BasCust::getCess,basCust.getCess())
                .set(MaterialExp::getModifyDate,LocalDateTime.now())
                .set(MaterialExp::getModifyStaffId,SecurityUtils.getUserId())
                .set(MaterialExp::getModifyStaffName,SecurityUtils.getUsername())
                .eq(BasCust::getCustId,basCust.getCustId());
        return success(basCustService.update(queryWrapper));
    }

    @DeleteMapping("/{custIds}")
    @Log(title = "供应商信息", businessType = BusinessType.DELETE)
    public AjaxResult deleteCust(@PathVariable Long[] custIds) {
        return toAjax(basCustService.removeByIds(Arrays.asList(custIds)));
    }

    @GetMapping("/updatePass/{custId}")
    @Log(title = "供应商信息", businessType = BusinessType.UPDATE)
    public void updatePass(@PathVariable Long custId) {
        BasCust byId = basCustService.getById(custId);
        LambdaUpdateWrapper<BasCust> updateWrapper = new UpdateWrapper<BasCust>()
                .lambda()
                .set(BasCust::getPassStaffId, SecurityUtils.getUserId())
                .set(BasCust::getPassStaffName, SecurityUtils.getUsername())
                .eq(BasCust::getCustId, custId);
        if (Objects.isNull(byId)) throw new ServiceException("供应商信息不存在");
        updateWrapper.set(byId.getPass() == 1, BasCust::getPass, 0)
                .set(byId.getPass() != 1, BasCust::getPass, 1)
                .set(byId.getPassDate() == null,BasCust::getPassDate, LocalDateTime.now());
        basCustService.update(updateWrapper);
    }
}
